package ebiz.buddhist.texts;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class CountSetting extends Activity {
	ArrayList<String> mListItem;
	ArrayAdapter<String> adspin;

	EditText mStartEdit, mEndEdit, mPeriodEdit;
	//CheckBox mSound;
	TextView CheckSound;
	RadioGroup SoundSelect;
	Spinner spinSound;
	int start, end, period;
	boolean sound;
	String FileName,RealFile;
	//TextView mNowPageText, mRemainText;


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.countsetting);
		
		SharedPreferences pref = getSharedPreferences("CountSet", 0);
		

		mListItem =  searching();

		
		int mSoundSelect = pref.getInt("mSoundSelect", 0);
		FileName = pref.getString("mSpinSelect", "파일지정");
		mListItem.add(0,FileName);
		
		findViewById(R.id.btnsave).setOnClickListener(mClickListener);
		findViewById(R.id.btncancel).setOnClickListener(mClickListener);

		mStartEdit = (EditText)findViewById(R.id.edstart);
		mEndEdit = (EditText)findViewById(R.id.edend);
		mPeriodEdit = (EditText)findViewById(R.id.edperiod);
	//	mSound = (CheckBox)findViewById(R.id.chksound);
		CheckSound = (TextView)findViewById(R.id.msg_checksound);
		spinSound = (Spinner)findViewById(R.id.spinner_select);
		spinSound.setPrompt("배경음악 파일을 지정해 주세요.");
		
		adspin = new ArrayAdapter<String>(this, 
				android.R.layout.simple_spinner_item, mListItem);
		adspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinSound.setAdapter(adspin);
		
		

		SoundSelect = (RadioGroup)findViewById(R.id.opt_backsound);
		SoundSelect.setOnCheckedChangeListener(mRadioCheck);


	//	mNowPageText = (TextView)findViewById(R.id.nowpage);
	//	mRemainText = (TextView)findViewById(R.id.remain);
		
		
	//	FileName = pref.getString("mSpinSelect", "파일지정");

		start = pref.getInt("start", 0);
		end = pref.getInt("end", 108);
		period = pref.getInt("period", 12);
		sound = pref.getBoolean("sound", false);
		
		switch (mSoundSelect) {
		case 0:
			SoundSelect.check(R.id.opt_backsound1);
			CheckSound.setVisibility(View.GONE);
			spinSound.setVisibility(View.GONE);

			break;
		case 1:
			SoundSelect.check(R.id.opt_backsound2);
			CheckSound.setVisibility(View.VISIBLE);
			spinSound.setVisibility(View.GONE);
			break;
		case 2:
			SoundSelect.check(R.id.opt_backsound3);
			CheckSound.setVisibility(View.GONE);
			spinSound.setVisibility(View.VISIBLE);
			break;
		}
		spinSound.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view, 
					int position, long id) {
				FileName = adspin.getItem(position);
				
			}
			public void onNothingSelected(AdapterView<?> parent) {
			}
		});
		mStartEdit.setText(Integer.toString(start));
		mEndEdit.setText(Integer.toString(end));
		mPeriodEdit.setText(Integer.toString(period));
		
		
	//	mSound.setChecked(sound);
		
	//	mSound.setVisibility(View.GONE); // VISIBLE

	}
	
	RadioGroup.OnCheckedChangeListener mRadioCheck = 
			new RadioGroup.OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (group.getId() == R.id.opt_backsound) {
					switch (checkedId) {
					case R.id.opt_backsound1:
						CheckSound.setVisibility(View.GONE);
						spinSound.setVisibility(View.GONE);
						break;
					case R.id.opt_backsound2:
						CheckSound.setVisibility(View.VISIBLE);
						spinSound.setVisibility(View.GONE);
						break;
					case R.id.opt_backsound3:
						CheckSound.setVisibility(View.GONE);
						spinSound.setVisibility(View.VISIBLE);
						break;
					}
				}
			}
		};
	View.OnClickListener mClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnsave:
				int mSoundSelect = 0;
				switch(SoundSelect.getCheckedRadioButtonId())
				{
				case R.id.opt_backsound1:
					mSoundSelect = 0;
					sound = false;
					break;
				case R.id.opt_backsound2:
					mSoundSelect = 1;
					sound = true;
					break;
				case R.id.opt_backsound3:
					mSoundSelect = 2;
					sound = false;
					break;
				}
				
				
				try {
					start = Integer.parseInt(mStartEdit.getText().toString());
				}
				catch (NumberFormatException e) {
					start = -1;
				}
				try {
					end = Integer.parseInt(mEndEdit.getText().toString());
				}
				catch (NumberFormatException e) {
					end = -1;
				}
				try {
					period = Integer.parseInt(mPeriodEdit.getText().toString());
				}
				catch (NumberFormatException e) {
					period = 12;
				}
				
				if((start >= 0 && start < end) && (end > 0) && (period > 0))
				{
					//sound = mSound.isChecked();
					SharedPreferences pref = getSharedPreferences("CountSet", 0);
					SharedPreferences.Editor editor = pref.edit();
					if(mSoundSelect == 2)
					{
						editor.putString("mSpinSelect", FileName);
					}
					editor.putInt("mSoundSelect", mSoundSelect);
					editor.putInt("start", start);
					editor.putInt("end", end);
					editor.putInt("period", period);
					editor.putBoolean("sound", sound);
					editor.commit();
					
					setResult(RESULT_OK, new Intent());
					finish();
					startActivity(new Intent(CountSetting.this, CountMain.class));
				}
				else
				{
					Toast.makeText(CountSetting.this, 
						//	getString(R.string.message_error),
							"설정 오류! 수행횟수 , 빈칸을 확인 해주세요",
							Toast.LENGTH_LONG).show();
				}
					
				break;
			case R.id.btncancel:
				setResult(RESULT_CANCELED, new Intent());
				finish();
				startActivity(new Intent(CountSetting.this, CountMain.class));
				break;
			}
		}
	};
	
	public  ArrayList<String> searching() {
		ArrayList<String> searchList = new ArrayList<String>();		
		String strCompare=null;
		ArrayList<String> sortList = new ArrayList<String>();
		File SDCardRoot = Environment.getExternalStorageDirectory(); 
        File dir = new File(SDCardRoot + "/buddhist/");
		if(dir!=null) {
			File[] dirs = dir.listFiles();
			if(dirs!=null) {
				for(int i=0; i<dirs.length; i++) {
					if(dirs[i].getName().endsWith("mp3")) {
					//	searchList.add(dirs[i].getAbsolutePath());		
					//	sortStr[i] = dirs[i].getName();
						strCompare = mpReNameHan(dirs[i].getName());
						sortList.add(strCompare);
					}
				}
			}
		}
		// 리스트를 배열로 변환
	     Object[] obj = sortList.toArray();
	     // API에서 Arrays.sort 참조 ( 정렬의 실체임 )
	     Arrays.sort(obj, new Comparator<Object>() {
	          public int compare(Object o1, Object o2) {
	               String t1 = (String) o1;
	               String t2 = (String) o2;
	               return t1.compareTo(t2);
	          }
	     });
	          
	     for (int i = 0; i < obj.length; i++) {
	    	 searchList.add((String) obj[i]);
	    	 }
		return  searchList;
	}
	
	 public String mpReNameHan(String str)
	 {
		   String strCompare = str;
		   if(strCompare.compareTo("chunsu.mp3")== 0)
			   strCompare = getString(R.string.item_chunsu);
		   if(strCompare.compareTo("heartsutra.mp3")== 0)
			   strCompare = getString(R.string.item_sutra);
		   if(strCompare.compareTo("beopseong.mp3")== 0)
			   strCompare = getString(R.string.item_beopseong);
		   if(strCompare.compareTo("hwaeom.mp3")== 0)
			   strCompare = getString(R.string.item_hwaeom);
		   if(strCompare.compareTo("sinju.mp3") == 0)
			   strCompare = getString(R.string.item_sinju);
//---------------------------------------------------------
		   if(strCompare.compareTo("kanmu.mp3") == 0)
			   strCompare = getString(R.string.item_kanmu);
		   if(strCompare.compareTo("bodalbomun.mp3") == 0)
			   strCompare = getString(R.string.item_bodalbomun);
		   if(strCompare.compareTo("kanja.mp3") == 0)
			   strCompare = getString(R.string.item_kanja);		  
//---------------------------------------------------------
		   
		   if(strCompare.compareTo("gwaneum.mp3") == 0)
			   strCompare = getString(R.string.item_gwaneum);
		   if(strCompare.compareTo("ami.mp3") == 0)
			   strCompare = getString(R.string.item_ami);				   
		   if(strCompare.compareTo("kukrak.mp3") == 0)
			   strCompare = getString(R.string.item_kukrak);		  
		   if(strCompare.compareTo("musang.mp3") == 0)
			   strCompare = getString(R.string.item_musang);
		   if(strCompare.compareTo("kang1.mp3") == 0)
			   strCompare = getString(R.string.item_kang1);
		   if(strCompare.compareTo("kang2.mp3") == 0)
			   strCompare = getString(R.string.item_kang2);
		   if(strCompare.compareTo("kang3.mp3") == 0)
			   strCompare = getString(R.string.item_kang3);
		   if(strCompare.compareTo("kang4.mp3") == 0)
			   strCompare = getString(R.string.item_kang4);
		   if(strCompare.compareTo("han_heartsutra.mp3") == 0)
			   strCompare = getString(R.string.item_kor_heartsutra);
		   if(strCompare.compareTo("han_chunsu.mp3") == 0)
			   strCompare = getString(R.string.item_kor_chunsu);
		   if(strCompare.compareTo("chunji.mp3") == 0)
			   strCompare = getString(R.string.item_chunji);
		   if(strCompare.compareTo("sinmyo.mp3") == 0)
			   strCompare = getString(R.string.item_sinmyo);
		   if(strCompare.compareTo("yebull.mp3")==0)
			   strCompare = getString(R.string.item_yebull);
		   if(strCompare.compareTo("prajna.mp3")==0)
			   strCompare = getString(R.string.item_prajna);
		   
		   if(strCompare.compareTo("108_7.mp3") == 0)
			   strCompare = getString(R.string.item_bows07);
		   if(strCompare.compareTo("108_8.mp3") == 0)
			   strCompare = getString(R.string.item_bows08);
		   if(strCompare.compareTo("108_9.mp3") == 0)
			   strCompare = getString(R.string.item_bows09);
		   if(strCompare.compareTo("108_10.mp3") == 0)
			   strCompare = getString(R.string.item_bows10);
		   if(strCompare.compareTo("108_11.mp3") == 0)
			   strCompare = getString(R.string.item_bows11);
		   if(strCompare.compareTo("108bow.mp3") == 0)
			   strCompare = getString(R.string.item_bows_s);
		   if(strCompare.compareTo("108bow_youngin.mp3") == 0)
			   strCompare = getString(R.string.item_bows_youngin);
		   
		   if(strCompare.compareTo("MedicineBuddha.mp3") == 0)
			   strCompare = getString(R.string.item_medicine_buddha);

			   if(strCompare.compareTo("MantraOfBodhisattvaCundi.mp3") == 0)
			   strCompare = getString(R.string.cundi);

			   if(strCompare.compareTo("Monks_of_Tibet_Healing.mp3") == 0)
			   strCompare = getString(R.string.monks);

			   if(strCompare.compareTo("Korean_Zen_master.mp3") == 0)
			   strCompare = getString(R.string.zen_master);

			   
			   if(strCompare.compareTo("chant.mp3") == 0)
			   strCompare = getString(R.string.item_chant);

			   if(strCompare.compareTo("followyourheart.mp3") == 0)
			   strCompare = getString(R.string.item_followyourheart);

			   if(strCompare.compareTo("windy_hermitage.mp3") == 0)
			   strCompare = getString(R.string.item_windy_hermitage);

			   if(strCompare.compareTo("healing_music.mp3") == 0)
			   strCompare = getString(R.string.item_healing_music);
			   
			   if(strCompare.compareTo("Buddhist_hymn3.mp3") == 0)
			   strCompare = getString(R.string.Buddhist_hymn_3);

	
			   if(strCompare.compareTo("diamond_sutra.mp3") == 0)
				   strCompare = getString(R.string.item_diamond) ;
			   
			   if(strCompare.compareTo("sakyamuni.mp3") == 0)
				   strCompare = getString(R.string.item_sakyamuni);
			 
			   if(strCompare.compareTo("guan.mp3") == 0)
				   strCompare = getString(R.string.item_guan);
			   
			   
			   if(strCompare.compareTo("jeongmok_1.mp3") == 0)
				   strCompare = getString(R.string.mp3_meditation);
			   
			   if(strCompare.compareTo("jeongmok_2.mp3") == 0)
				   strCompare = getString(R.string.mp3_meditation_2);
		   
		   return strCompare;
	 }
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	  if(event.getAction() == KeyEvent.ACTION_DOWN){
	   if(keyCode == KeyEvent.KEYCODE_BACK){
		   finish();
			startActivity(new Intent(CountSetting.this, CountMain.class));
		   return true;
	   }
	  }
	  return false;
	 }

}
